﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace AioApp.Views
{
    public partial class UserForm : Form
    {

        private AioDataSetTableAdapters.UsuarioTableAdapter userTableAdapter = new AioDataSetTableAdapters.UsuarioTableAdapter();

        public UserForm()
        {
            InitializeComponent();
            userTableAdapter.Fill(aioDataSet1.Usuario);

            //this.bindingSource1.CurrentChanged += new EventHandler(bindingSource1_CurrentChanged);
            
 
             
            
            //aioDataSet1.Usuario.UsuarioRowChanged += new AioDataSet.UsuarioRowChangeEventHandler(Usuario_UsuarioRowChanged);
            //aioDataSet1.Usuario.UsuarioRowChanging += new AioDataSet.UsuarioRowChangeEventHandler(Usuario_UsuarioRowChanging);
            //aioDataSet1.Usuario.TableNewRow += new DataTableNewRowEventHandler(Usuario_TableNewRow);
            //aioDataSet1.Usuario.ColumnChanged += new DataColumnChangeEventHandler(Usuario_ColumnChanged);
            //aioDataSet1.Usuario.ColumnChanging += new DataColumnChangeEventHandler(Usuario_ColumnChanging);
        }

        void Adapter_RowUpdated(object sender, System.Data.SqlServerCe.SqlCeRowUpdatedEventArgs e)
        {
            
        }

        void Adapter_RowUpdating(object sender, System.Data.SqlServerCe.SqlCeRowUpdatingEventArgs e)
        {
            
        }

        void Usuario_ColumnChanged(object sender, DataColumnChangeEventArgs e)
        {
        }

        void Usuario_ColumnChanging(object sender, DataColumnChangeEventArgs e)
        {
        }

        void Usuario_UsuarioRowChanging(object sender, AioDataSet.UsuarioRowChangeEvent e)
        {
            
        }

        void Usuario_UsuarioRowChanged(object sender, AioDataSet.UsuarioRowChangeEvent e)
        {
            
            
            
        }

        void Usuario_RowChanged(object sender, DataRowChangeEventArgs e)
        {
        }

        void Usuario_RowChanging(object sender, DataRowChangeEventArgs e)
        {
            
        }

        void bindingSource1_CurrentChanged(object sender, EventArgs e)
        {
            
            if (listBox1.SelectedItem!=null)
            {
                switch (((AioApp.AioDataSet.UsuarioRow)((DataRowView)listBox1.SelectedItem).Row).RowState)
                {
                    case DataRowState.Modified:
                        updateButton.Enabled = true;
                        break;
                    case DataRowState.Added:
                        updateButton.Enabled = true;
                        break;
                    case DataRowState.Unchanged:
                        updateButton.Enabled = false;
                        break;
                }
            }
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //DataRow nr = aioDataSet1.Usuario.NewUsuarioRow();
            //aioDataSet1.Usuario.Rows.Add(nr);
            //listBox1.SelectedIndex = listBox1.Items.Count - 1;
            bindingSource1.AddNew();
            updateButton.Enabled = true;
        }


        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            
            aioDataSet1.Dispose();
            base.OnFormClosing(e);
        }

        private void updateButton_Click(object sender, EventArgs e)
        {
            if (passwordCheck() == false)
            {
                MessageBox.Show("Invalid Password or different");
                return;
            }
            string md5 = Bll.Util.Instance.getMD5(passwordTextBox.Text);

            //aioDataSet1.Usuario.AddUsuarioRow(nameTextBox.Text, emailTextBox.Text, md5);


            this.Validate();
            this.bindingSource1.EndEdit();
            userTableAdapter.Update(aioDataSet1.Usuario);
            updateButton.Enabled = false;
            //this.tableAdapterManager.UpdateAll(this.aioDataSet);

            //if (((AioApp.AioDataSet.UsuarioRow)((DataRowView)listBox1.SelectedItem).Row).RowState == DataRowState.Added)
            //{
            //    MessageBox.Show("Added");
            //}
            //if (((AioApp.AioDataSet.UsuarioRow)((DataRowView)listBox1.SelectedItem).Row).RowState == DataRowState.Modified)
            //{
            //    MessageBox.Show("Modified");
            //}

        }

        private bool passwordCheck()
        {
            if (passwordCheckTextBox.Text == String.Empty)
            {
                return false;
            }

            if (passwordCheckTextBox.Text != passwordTextBox.Text)
            {
                return false;
            }

            return true;
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (bindingSource1.Current != null)
            {
                bindingSource1.RemoveCurrent();
                userTableAdapter.Update(aioDataSet1.Usuario);
            }

            
            //((AioApp.AioDataSet.UsuarioRow)((DataRowView)listBox1.SelectedItem).Row).Delete();
            
            //Line beneath doesn't work
            //aioDataSet1.Usuario.RemoveUsuarioRow(((AioApp.AioDataSet.UsuarioRow)((DataRowView)listBox1.SelectedItem).Row));
        }

        private void UserForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'aioDataSet1.Usuario' table. You can move, or remove it, as needed.
            this.userTableAdapter.Fill(this.aioDataSet1.Usuario);

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            ((AioApp.AioDataSet.UsuarioRow)((DataRowView)listBox1.SelectedItem).Row).BeginEdit();
            updateButton.Enabled = true;
        }
    }
}
